// packageAPI/pages/device/bluetooth/bluetoothLowEnergy/BLE.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[{
      label:'设备名称',
      prop:'name',
      type:'text'
    },{
      label:'设备 id',
      prop:'deviceId',
      type:'text'
    },{
      label:'信号强度',
      prop:'RSSI',
      type:'text'
    },{
      label:'ServiceUUIDs',
      prop:'advertisServiceUUIDs',
      type:'picker',
      labelOptions:[],
      valueOptions:[]
    },{
      label:'LocalName',
      prop:'localName',
      type:'text'
    }],
    title:'蓝牙低功耗',
    formData:{

    },
    result:'',
    isConnect:false,
    characteristicId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('bluetoothData', (data)=>{
      const uuid = data.advertisServiceUUIDs ? data.advertisServiceUUIDs[0]:''
      console.log(data,uuid)
      this.setData({
        formData:data,
        // 'formData.advertisServiceUUIDs':uuid,
        result:data,
        'schema[3].labelOptions':data.advertisServiceUUIDs,
        'schema[3].valueOptions':data.advertisServiceUUIDs,
      })
      this.setData({
        'formData.advertisServiceUUIDs':uuid
      })
    })
    let that = this
    wx.onBLECharacteristicValueChange(function(characteristic) {
      console.log('characteristic value comed:', characteristic)
      that.setData({
        result:characteristic
      })
    })
    wx.onBLEMTUChange(function (res) {
      wx.showToast({
        title: `${res.deviceId}:${res.mtu}`,
      })
    })
    wx.onBLEConnectionStateChange(function(res) {
      // 该方法回调中可以用于处理连接意外断开等异常情况
      wx.showToast({
        title: `${res.deviceId}:${res.connected}`,
      })
    })
  },
  getFormData(e){
    this.setData({
      formData:e.detail.formData
    })
  },
  createBLEConnection() {
    const deviceId = this.data.formData.deviceId
    wx.showLoading({
      title: '连接中',
    })
    wx.createBLEConnection({
      deviceId,
      timeout:'20000',
      success:(e)=>{
        this.setData({
          isConnect:true
        })
      },
      complete:(e)=>{
        wx.hideLoading()
        this.setData({
          result:e
        })
      }
    })
  },
  getBLEMTU() {
    const deviceId = this.data.formData.deviceId
    wx.getBLEMTU({
      deviceId,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  setBLEMTU() {
    const deviceId = this.data.formData.deviceId
    wx.setBLEMTU({
      deviceId,
      mtu:50,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  getBLEDeviceServices() {
    const deviceId = this.data.formData.deviceId
    wx.getBLEDeviceServices({
      deviceId,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  getBLEDeviceRSSI() {
    const deviceId = this.data.formData.deviceId
    wx.getBLEDeviceRSSI({
      deviceId,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  getBLEDeviceCharacteristics() {
    const deviceId = this.data.formData.deviceId
    const serviceId = this.data.formData.advertisServiceUUIDs
    wx.getBLEDeviceCharacteristics({
      deviceId,
      serviceId,
      success:(e)=>{
        this.setData({
          characteristicId:e.characteristics[0].uuid||''
        })
      },
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  readBLECharacteristicValue(){
    const deviceId = this.data.formData.deviceId
    const serviceId = this.data.formData.advertisServiceUUIDs
    const characteristicId = this.data.characteristicId
    wx.readBLECharacteristicValue({
      deviceId,
      serviceId,
      characteristicId,
      fail:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  writeBLECharacteristicValue(){
    const deviceId = this.data.formData.deviceId
    const serviceId = this.data.formData.advertisServiceUUIDs
    const characteristicId = this.data.characteristicId
    let buffer = new ArrayBuffer(1)
    let dataView = new DataView(buffer)
    dataView.setUint8(0, 0)
    wx.writeBLECharacteristicValue({
      deviceId,
      serviceId,
      characteristicId,
      value:buffer,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  notifyBLECharacteristicValueChange(){
    const deviceId = this.data.formData.deviceId
    const serviceId = this.data.formData.advertisServiceUUIDs
    const characteristicId = this.data.characteristicId
    wx.notifyBLECharacteristicValueChange({
      deviceId,
      serviceId,
      characteristicId,
      state:true,
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  },
  offBLEMTUChange(){
    wx.offBLEMTUChange()
  },
  offBLEConnectionStateChange(){
    wx.offBLEConnectionStateChange()
  },
  offBLECharacteristicValueChange(){
    wx.offBLECharacteristicValueChange()
  },
  closeBLEConnection() {
    const deviceId = this.data.formData.deviceId
    wx.closeBLEConnection({
      deviceId,
      success:(e)=>{
        this.setData({
          isConnect:false
        })
      },
      complete:(e)=>{
        this.setData({
          result:e
        })
      }
    })
  }
})